package com.pktechan.manage.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pktechan.manage.entity.CommissionBalance;
import com.pktechan.manage.entity.CommissionWithdrawFlow;
import com.pktechan.manage.entity.SysUser;
import com.pktechan.manage.exception.CustomException;
import com.pktechan.manage.service.CommissionBalanceService;
import com.pktechan.manage.service.CommissionGenerateFlowService;
import com.pktechan.manage.service.CommissionWithdrawFlowService;
import com.pktechan.manage.utils.HtmlUtil;
import com.pktechan.manage.utils.SessionUtils;
import com.pktechan.manage.vo.CommissionBalanceVo;
import com.pktechan.manage.vo.CommissionGenerateFlowVo;
import com.pktechan.manage.vo.CommissionWithdrawFlowVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @program: cloudfloating
 * @description: 佣金相关功能
 * @author: s.zhang
 * @create: 2021-03-10 09:21
 **/
@Controller
@RequestMapping("commissionBalanceController")
public class CommissionBalanceController {

    private static final Logger logger = LoggerFactory.getLogger(CommissionBalanceController.class);

    @Autowired
    private CommissionBalanceService commissionBalanceService;
    @Autowired
    private CommissionGenerateFlowService commissionGenerateFlowService;
    @Autowired
    private CommissionWithdrawFlowService commissionWithdrawFlowService;


    /**
     * 查询客户佣金管理列表
     *
     * @param commissionBalanceVo
     * @param response
     */
    @RequestMapping(value = "getCommissionBalanceVoList", method = RequestMethod.POST)
    public void getCommissionBalanceVoList(@RequestBody CommissionBalanceVo commissionBalanceVo, HttpServletResponse response) {
        try {
            logger.info("查询客户佣金管理列表");
            PageHelper.startPage(commissionBalanceVo.getPageNo(), commissionBalanceVo.getPageSize());
            List<CommissionBalanceVo> list = commissionBalanceService.getCommissionBalanceVoList(commissionBalanceVo);
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response, pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }
    /**
     * 查询客户佣金流水管理列表
     *
     * @param commissionGenerateFlowVo
     * @param response
     */
    @RequestMapping(value = "getCommissionGenerateFlowVoList", method = RequestMethod.POST)
    public void getCommissionGenerateFlowVoList(@RequestBody CommissionGenerateFlowVo commissionGenerateFlowVo, HttpServletResponse response) {
        try {
            logger.info("查询客户佣金流水管理列表");
            PageHelper.startPage(commissionGenerateFlowVo.getPageNo(), commissionGenerateFlowVo.getPageSize());
            List<CommissionGenerateFlowVo> list = commissionGenerateFlowService.getCommissionGenerateFlowVoList(commissionGenerateFlowVo);
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response, pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }
    /**
     * 查询客户佣金提现管理列表
     *
     * @param commissionWithdrawFlowVo
     * @param response
     */
    @RequestMapping(value = "getCommissionWithdrawFlowVoList", method = RequestMethod.POST)
    public void getCommissionWithdrawFlowVoList(@RequestBody CommissionWithdrawFlowVo commissionWithdrawFlowVo, HttpServletResponse response) {
        try {
            logger.info("查询客户佣金提现管理列表");
            PageHelper.startPage(commissionWithdrawFlowVo.getPageNo(), commissionWithdrawFlowVo.getPageSize());
            List<CommissionWithdrawFlowVo> list = commissionWithdrawFlowService.getCommissionWithdrawFlowVoList(commissionWithdrawFlowVo);
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response, pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 打开客户佣金管理页面
     * @return
     */
    @RequestMapping("/toCommissionBalanceVoList")
    public String toCommissionBalanceVoList() {
        return "commission/balanceVoList";
    }

    /**
     * 客户佣金流水管理
     * @return
     */
    @RequestMapping("/toCommissionGenerateFlowVoList")
    public String toCommissionGenerateFlowVoList() {
        return "commission/generateFlowVoList";
    }

    /**
     * 客户佣金提现管理
     * @return
     */
    @RequestMapping("/toCommissionWithdrawFlowVoList")
    public String toCommissionWithdrawFlowVoList() {
        return "commission/withdrawFlowVoList";
    }

    /**
     * 客户佣金提现详情
     *
     * @return
     */
    @RequestMapping("/toCommissionWithdrawDetail")
    public String toCommissionWithdrawDetail() {
        return "commission/commissionWithdrawDetail";
    }

    /**
     * 根据id查询客户佣金提现详情
     *
     * @param commissionWithdrawFlow
     * @param response
     */
    @RequestMapping(value = "getCommissionWithdrawDetailById", method = RequestMethod.POST)
    public void getCommissionWithdrawDetailById(@RequestBody CommissionWithdrawFlow commissionWithdrawFlow, HttpServletResponse response) {
        try {
            logger.info("根据id=" + commissionWithdrawFlow.getId() + "查询客户佣金提现详情");
            CommissionWithdrawFlow byId = commissionWithdrawFlowService.getById(commissionWithdrawFlow.getId());
            HtmlUtil.writerSuccessJson(response, byId);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 根据id修改佣金提现状态
     *
     * @param commissionWithdrawFlow
     * @param response
     */
    @RequestMapping(value = "updateCommissionWithdrawStatusById", method = RequestMethod.POST)
    public void updateCommissionWithdrawStatusById(@RequestBody CommissionWithdrawFlow commissionWithdrawFlow, HttpServletRequest request, HttpServletResponse response) {
        try {
            SysUser user = SessionUtils.getUser(request);
            logger.info("根据id"+commissionWithdrawFlow.getId()+"修改佣金提现状态");
            commissionWithdrawFlowService.updateCommissionWithdrawStatusById(commissionWithdrawFlow,user.getId());


            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }
    /**
     * 根据Id修改入金冻结金额并解冻
     *
     * @param commissionBalance
     * @param response
     */
    @RequestMapping(value = "updateFreeiznfThwaingById", method = RequestMethod.POST)
    public void updateFreeiznfThwaingById(@RequestBody CommissionBalance commissionBalance, HttpServletRequest request, HttpServletResponse response) {
        try {
            logger.info("根据Id="+commissionBalance.getId()+"修改入金冻结金额并解冻");
            commissionBalanceService.updateFreeiznfThwaingById(commissionBalance);
            HtmlUtil.writerSuccessJson(response);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }
}
